import { UserConfig, ConfigEnv, loadEnv, defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import UnoCSS from 'unocss/vite'
const pathSrc = path.resolve(__dirname, 'src')
import {
    name,
    version,
    engines,
    dependencies,
    devDependencies,
} from './package.json'
const __APP_INFO__ = {
    pkg: { name, version, engines, dependencies, devDependencies },
    buildTimestamp: Date.now(),
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }: ConfigEnv): UserConfig => {
    const env = loadEnv(mode, process.cwd())
    return {
        plugins: [
            vue(),
            AutoImport({
                imports: [
                    'vue',
                    '@vueuse/core',
                    'pinia',
                    'vue-router',
                    'vue-i18n',
                ],
                resolvers: [
                    ElementPlusResolver(),
                    // 自动导入图标组件
                    IconsResolver({}),
                ],
                dts: path.resolve(pathSrc, 'types', 'auto-imports.d.ts'), // 自动导入组件类型声明文件位置，默认根目录
            }),
            Components({
                resolvers: [
                    ElementPlusResolver(),
                    // 自动注册图标组件
                    IconsResolver({
                        enabledCollections: ['ep'], // element-plus图标库，其他图标库 https://icon-sets.iconify.design/
                    }),
                ],
                // 指定自定义组件位置(默认:src/components)
                dirs: ['src/components', 'src/**/components'],
                dts: path.resolve(pathSrc, 'types', 'components.d.ts'), //  自动导入组件类型声明文件位置，默认根目录
            }),
            Icons({
                // 自动安装图标库
                autoInstall: true,
            }),
            createSvgIconsPlugin({
                // 指定需要缓存的图标文件夹
                iconDirs: [path.resolve(process.cwd(), 'src/assets/icons')],
                // 指定symbolId格式
                // symbolId: 'icon-[dir]-[name]',
            }),
            UnoCSS({
                /* options */
            }),
        ],
        resolve: {
            alias: {
                '@': pathSrc,
            },
        },
        css: {
            // CSS 预处理器
            preprocessorOptions: {
                //define global scss variable
                scss: {
                    javascriptEnabled: true,
                    additionalData: `@use "@/styles/variables.scss" as *;`,
                },
            },
        },
        define: {
            __APP_INFO__: JSON.stringify(__APP_INFO__),
        },
        server: {
            // 允许IP访问
            host: '0.0.0.0',
            // 应用端口 (默认:3000)
            port: Number(env.VITE_APP_PORT),
            // 运行是否自动打开浏览器
            open: true,
            proxy: {
                /** 代理前缀为 /dev-api 的请求  */
                [env.VITE_APP_BASE_API]: {
                    changeOrigin: true,
                    // 接口地址
                    target: env.VITE_APP_API_URL,
                    rewrite: (path) =>
                        path.replace(
                            new RegExp('^' + env.VITE_APP_BASE_API),
                            ''
                        ),
                },
            },
        },
    }
})
